/*
 * drivers/video/sunxi/disp2/disp/lcd/default_panel.c
 *
 * Copyright (c) 2007-2019 Allwinnertech Co., Ltd.
 * Author: zhengxiaobin <zhengxiaobin@allwinnertech.com>
 *
 * This software is licensed under the terms of the GNU General Public
 * License version 2, as published by the Free Software Foundation, and
 * may be copied, distributed, and modified under those terms.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 */
#include "default_panel.h"

static void LCD_power_on(u32 sel);
static void LCD_power_off(u32 sel);
static void LCD_bl_open(u32 sel);
static void LCD_bl_close(u32 sel);

static void LCD_panel_init(u32 sel);
static void LCD_panel_exit(u32 sel);
#define panel_reset(sel, val) sunxi_lcd_gpio_set_value(sel, 0, val)

static void LCD_cfg_panel_info(panel_extend_para * info)
{
	u32 i = 0, j=0;
	u32 items;
	u8 lcd_gamma_tbl[][2] =
	{
		//{input value, corrected value}
		{0, 0},
		{15, 15},
		{30, 30},
		{45, 45},
		{60, 60},
		{75, 75},
		{90, 90},
		{105, 105},
		{120, 120},
		{135, 135},
		{150, 150},
		{165, 165},
		{180, 180},
		{195, 195},
		{210, 210},
		{225, 225},
		{240, 240},
		{255, 255},
	};

	u32 lcd_cmap_tbl[2][3][4] = {
	{
		{LCD_CMAP_G0,LCD_CMAP_B1,LCD_CMAP_G2,LCD_CMAP_B3},
		{LCD_CMAP_B0,LCD_CMAP_R1,LCD_CMAP_B2,LCD_CMAP_R3},
		{LCD_CMAP_R0,LCD_CMAP_G1,LCD_CMAP_R2,LCD_CMAP_G3},
		},
		{
		{LCD_CMAP_B3,LCD_CMAP_G2,LCD_CMAP_B1,LCD_CMAP_G0},
		{LCD_CMAP_R3,LCD_CMAP_B2,LCD_CMAP_R1,LCD_CMAP_B0},
		{LCD_CMAP_G3,LCD_CMAP_R2,LCD_CMAP_G1,LCD_CMAP_R0},
		},
	};

	items = sizeof(lcd_gamma_tbl)/2;
	for (i=0; i<items-1; i++) {
		u32 num = lcd_gamma_tbl[i+1][0] - lcd_gamma_tbl[i][0];

		for (j=0; j<num; j++) {
			u32 value = 0;

			value = lcd_gamma_tbl[i][1] + ((lcd_gamma_tbl[i+1][1] - lcd_gamma_tbl[i][1]) * j)/num;
			info->lcd_gamma_tbl[lcd_gamma_tbl[i][0] + j] = (value<<16) + (value<<8) + value;
		}
	}
	info->lcd_gamma_tbl[255] = (lcd_gamma_tbl[items-1][1]<<16) + (lcd_gamma_tbl[items-1][1]<<8) + lcd_gamma_tbl[items-1][1];

	memcpy(info->lcd_cmap_tbl, lcd_cmap_tbl, sizeof(lcd_cmap_tbl));

}

static s32 LCD_open_flow(u32 sel)
{
	LCD_OPEN_FUNC(sel, LCD_power_on, 30);   //open lcd power, and delay 50ms
	LCD_OPEN_FUNC(sel, LCD_panel_init, 50);   //open lcd power, than delay 200ms
	LCD_OPEN_FUNC(sel, sunxi_lcd_tcon_enable, 100);     //open lcd controller, and delay 100ms
	LCD_OPEN_FUNC(sel, LCD_bl_open, 0);     //open lcd backlight, and delay 0ms
	return 0;
}

static s32 LCD_close_flow(u32 sel)
{
	LCD_CLOSE_FUNC(sel, LCD_bl_close, 0);       //close lcd backlight, and delay 0ms
	LCD_CLOSE_FUNC(sel, sunxi_lcd_tcon_disable, 0);         //close lcd controller, and delay 0ms
	LCD_CLOSE_FUNC(sel, LCD_panel_exit,	200);   //open lcd power, than delay 200ms
	LCD_CLOSE_FUNC(sel, LCD_power_off, 500);   //close lcd power, and delay 500ms

	return 0;
}

static void LCD_power_on(u32 sel)
{
	sunxi_lcd_power_enable(sel, 0);
	sunxi_lcd_delay_ms(10);
	sunxi_lcd_power_enable(sel, 1);
	sunxi_lcd_delay_ms(10);
	sunxi_lcd_pin_cfg(sel, 1);
	sunxi_lcd_delay_ms(50);
}

static void LCD_power_off(u32 sel)
{
	sunxi_lcd_pin_cfg(sel, 0);
	sunxi_lcd_delay_ms(20);
	panel_reset(sel, 0);
	sunxi_lcd_delay_ms(5);
	sunxi_lcd_power_disable(sel, 1);
	sunxi_lcd_delay_ms(5);
	sunxi_lcd_power_disable(sel, 0);
}

static void LCD_bl_open(u32 sel)
{
	sunxi_lcd_pwm_enable(sel);
	sunxi_lcd_backlight_enable(sel);
}

static void LCD_bl_close(u32 sel)
{
	sunxi_lcd_backlight_disable(sel);
	sunxi_lcd_pwm_disable(sel);
}


#define REGFLAG_DELAY         0XFE
#define REGFLAG_END_OF_TABLE  0xFC   /* END OF REGISTERS MARKER */

struct LCM_setting_table {
	u8 cmd;
	u32 count;
	u8 para_list[100];
};

static struct LCM_setting_table lcm_d300t9307v0_setting[] = {
    {0xB9, 3,  {0xF1, 0x12, 0x83}},
	{0xB1, 5,  {0x00,0x00,0x00,0xDA,0x80}},
	{0xB2, 3,  {0x00, 0x13, 0x70}},
	{0xB3, 10, {0x10,0x10,0x28,0x28,0x03,0xFF,0x00,0x00,0x00,0x00}},
	{0xB4, 1,  {0x80}},
	{0xB5, 2,  {0x0A,0x0A}},
	{0xB6, 2,  {0x87,0x87}},
	{0xB8, 4,  {0x26,0x22,0xF0,0x63}},
	{0xBA, 27, {0x31,0x81,0x05,0xF9,0x0E,0x0E,0x20,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x44,0x25,0x00,0x90,0x0A,0x00,0x00,0x01,0x4F,0x01,0x00,0x00,0x37}},
	{0xBC, 1,  {0x47}},
	{0xBF, 3,  {0x02,0x11,0x00}},
	{0xC0, 9,  {0x73,0x73,0x50,0x50,0x00,0x00,0x12,0x50,0x00}},
	{0xC1, 12, {0x53,0xC0,0x32,0x32,0x77,0xE1,0xD,0xD,0x77,0x77,0x33,0x33}},
	{0xC6, 6,  {0x82,0x00,0xBF,0xFF,0x00,0xFF}},
	{0xC7, 6,  {0xB8,0x00,0x0A,0x00,0x00,0x00}},
	{0xC8, 4,  {0x10,0x40,0x1E,0x02}},
	{0xCC, 1,  {0x0B}},
	{0xE0, 34, {0x00,0x06,0x0A,0x2A,0x3D,0x3F,0x3B,0x37,0x06,0x0B,0x0C,0x10,0x11,0x10,0x13,0x12,0x18,0x00,0x06,0x0A,0x2A,0x3D,0x3F,0x3B,0x37,0x06,0x0B,0x0C,0x10,0x11,0x10,0x13,0x12,0x18}},
	{0xE3, 14, {0x07,0x07,0x0B,0x0B,0x0B,0x0B,0x00,0x00,0x00,0x00,0xFF,0x00,0xC0,0x10}},
	{0xE9, 63, {0xC8,0x10,0x02,0x00,0x00,0xB0,0xB1,0x11,0x31,0x23,0x28,0x80,0xB0,0xB1,0x27,0x08,0x00,0x04,0x02,0x00,0x00,0x00,0x00,0x04,0x02,0x00,0x00,0x00,0x88,0x88,0xBA,0x60,0x24,0x08,0x88,0x88,0x88,0x88,0x88,0x88,0x88,0xBA,0x71,0x35,0x18,0x88,0x88,0x88,0x88,0x88,0x00,0x00,0x00,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}},
	{0xEA, 61, {0x97,0x0A,0x82,0x02,0x03,0x07,0x00,0x00,0x00,0x00,0x00,0x00,0x81,0x88,0xBA,0x17,0x53,0x88,0x88,0x88,0x88,0x88,0x88,0x80,0x88,0xBA,0x06,0x42,0x88,0x88,0x88,0x88,0x88,0x88,0x23,0x00,0x00,0x01,0xA4,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}},
	{0xEF, 3,  {0xFF,0xFF,0x01}},
	//{0x36, 1,  {0xC0}},//上下翻转
	{0x35, 1,  {0x00}},//打开TE功能
	//{0x44, 2,  {0x00,0x0A}},	//TE功能调试
	{0x11 ,0, {}},	
	{REGFLAG_DELAY, REGFLAG_DELAY, {150} },
	{0x29 ,0, {}},
	{REGFLAG_DELAY, REGFLAG_DELAY, {50} },
	{REGFLAG_END_OF_TABLE, REGFLAG_END_OF_TABLE, {} }	
};

static void LCD_panel_init(u32 sel)
{
	u32 i;
	printk("st7703 reset\n");
	panel_reset(sel, 1);
	sunxi_lcd_delay_ms(1);
	panel_reset(sel, 0);
	sunxi_lcd_delay_ms(10);
	panel_reset(sel, 1);
	sunxi_lcd_delay_ms(150);
	printk("st7703 reset finish\n");

	sunxi_lcd_dsi_clk_enable(sel);
	sunxi_lcd_delay_ms(10);
	printk("st7703 init\n");
	
	for (i = 0;; i++) 
	{
		if (lcm_d300t9307v0_setting[i].cmd == REGFLAG_END_OF_TABLE)
			break;
		else if (lcm_d300t9307v0_setting[i].cmd == REGFLAG_DELAY)
			sunxi_lcd_delay_ms(lcm_d300t9307v0_setting[i].para_list[0]);
		else 
		{
			dsi_dcs_wr(sel, lcm_d300t9307v0_setting[i].cmd,
							lcm_d300t9307v0_setting[i].para_list,
							lcm_d300t9307v0_setting[i].count);
		}
	}
	printk("st7703 init finish\n");		
}

static void LCD_panel_exit(u32 sel)
{
	sunxi_lcd_dsi_dcs_write_0para(sel, 0x10);
	sunxi_lcd_delay_ms(80);
	sunxi_lcd_dsi_dcs_write_0para(sel, 0x28);
	sunxi_lcd_delay_ms(50);
}

//sel: 0:lcd0; 1:lcd1
static s32 LCD_user_defined_func(u32 sel, u32 para1, u32 para2, u32 para3)
{
	return 0;
}

__lcd_panel_t default_panel = {
	/* panel driver name, must mach the name of lcd_drv_name in sys_config.fex */
	.name = "default_lcd",
	.func = {
		.cfg_panel_info = LCD_cfg_panel_info,
		.cfg_open_flow = LCD_open_flow,
		.cfg_close_flow = LCD_close_flow,
		.lcd_user_defined_func = LCD_user_defined_func,
	},
};
